package com.mikao.service.impl;

import com.mikao.dao.FeedbackDao;
import com.mikao.dao.UserDao;
import com.mikao.dto.bo.ReqDataTableDto;
import com.mikao.dto.vo.FeedbackDto;
import com.mikao.dto.vo.RspDataTableDto;
import com.mikao.entity.Feedback;
import com.mikao.entity.User;
import com.mikao.service.FeedbackService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author kenny
 */

@Service
public class FeedbackServiceImpl implements FeedbackService {


    private FeedbackDao feedbackDao;
    private UserDao userDao;

    public FeedbackServiceImpl(FeedbackDao feedbackDao, UserDao userDao) {
        this.feedbackDao = feedbackDao;
        this.userDao = userDao;
    }

    @Override
    public RspDataTableDto<FeedbackDto> feedbackList(ReqDataTableDto dataTableDTO, String dateRangePicker) {
        Date start = null;
        Date end = null;
        if (dateRangePicker != null) {
            String[] time = dateRangePicker.split(" - ");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                start = simpleDateFormat.parse(time[0]);
                end = simpleDateFormat.parse(time[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long time1 = start != null ? start.getTime() : 0;
            long time2 = end != null ? end.getTime() : 0;
            if (time1 == time2) {
                if (end != null) {
                    end.setTime(time2 + 86399999);
                }
            }
        }

        Date finalStart = start;
        Date finalEnd = end;

        PageRequest pageRequest = new PageRequest(dataTableDTO.getPage(), dataTableDTO.getLength(),
                new Sort(Sort.Direction.DESC, "createTime"));
        Page<Feedback> page = feedbackDao.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.hasText(dateRangePicker)) {
                Predicate p = criteriaBuilder.between(root.get("createTime"), finalStart, finalEnd);
                predicates.add(p);
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        }, pageRequest);

        List<FeedbackDto> dtos = new ArrayList<>();
        FeedbackDto feedbackDto;
        User user;
        for (Feedback feedback : page.getContent()) {
            feedbackDto = new FeedbackDto();
            BeanUtils.copyProperties(feedback, feedbackDto);
            user = userDao.findOne(feedback.getUserId());
            if (!ObjectUtils.isEmpty(user)) {
                feedbackDto.setName(user.getName());
            }
            dtos.add(feedbackDto);
        }
        return new RspDataTableDto<>(dataTableDTO.getDraw(), page.getTotalElements(), dtos);
    }

    @Override
    public void deleteFeedback(long id) {
        feedbackDao.delete(id);
    }
}
